<?php

namespace Liuhelong\LaravelAdmin\Wechat\Models;

use Encore\Admin\Traits\DefaultDatetimeFormat;
use Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Storage;

use Illuminate\Database\Eloquent\Model;

class WechatOffiaccountTemplateMessage extends Model
{
    use DefaultDatetimeFormat;
	
	
	protected $primaryKey = 'template_id';
	public $incrementing = false;
	private $wechat;
	
	public function __construct(){
		parent::__construct();
		$this->wechat = app('liuhelong_wechat_service');
	}
	
	
	public function paginate()
    {
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);
        $start = ($page-1)*$perPage;

        $data = $this->wechat->handle('template_message','getPrivateTemplates');
		$list = static::hydrate(array_slice($data['template_list'],$start,$perPage));
        $paginator = new LengthAwarePaginator($list, count($data['template_list']), $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
	
	protected function findOrFail($id)
	{		
		return static::newFromBuilder([
			'template_id'=>$id
		]);
	}
	
	public function save(array $options = [])
	{
		$attributes = $this->getAttributes();
		$this->wechat->handle('template_message','addTemplate',[$attributes['shortId']]);
		
	}
	
	public function delete()
	{
		$this->wechat->handle('template_message','deletePrivateTemplate',[$this->getKey()]);
	}
}
